const app = getApp();
var host = app.globalData.host;
var utils = app.globalData.utils;
var self = null;

Page({
  data: {
    themeColor: '',
    defaultColor: '',
    borderColor: '',
    list: [],
    keyword: '',
    timeoutVar: null,
    pageNumber: 1,
    totalPage: 1,
    schoolType: 0, // 0本科院校 1硕士院校

    countryArray: [],
    countryIndex: 0
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
    self.init();
    self.setData({
      schoolType: Number(options.type)
    })
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  init() {
    wx.request({
      url: host + '/lx/mini/user/schoolCountries',
      success: function (res) {
        if (res.data.status == 200) {
          self.setData({
            countryArray: res.data.info
          })
        } else {
          console.log("error: " + res.data.msg);
        }
        self.load();
      }
    })
  },
  load() {
    wx.showLoading({
      title: '加载中'
    })
    wx.request({
      url: host + '/lx/mini/user/schools',
      data: {
        kw: self.data.keyword,
        country: self.data.countryArray[self.data.countryIndex],
        page: self.data.pageNumber
      },
      success: function (res) {
        if (res.data.status == 200) {
          self.setData({
            list: res.data.info.list,
            pageNumber: res.data.info.pageNumber,
            totalPage: res.data.info.totalPage
          });
        } else {
          console.log("error: " + res.data.msg);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },
  search(e) {
    self.setData({
      list: []
    })
    clearTimeout(self.data.timeoutVar);
    var t = setTimeout(function () {
      self.setData({
        keyword: e.detail.value,
        pageNumber: 1
      })
      self.load();
    }, 500);
    self.setData({
      timeoutVar: t
    })
  },
  clearKeyword() {
    self.setData({
      keyword: ''
    })
    self.load();
  },
  onFilterChange(e) {
    var index = Number(e.currentTarget.dataset.index);
    self.setData({
      countryIndex: index,
      pageNumber: 1
    })
    self.load();
  },
  selectSchool(e) {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    if (self.data.schoolType === 0) {
      prevPage.setData({
        undergradSchool: e.currentTarget.dataset.schoolid,
        undergradSchoolName: e.currentTarget.dataset.schoolname
      })
    } else if (self.data.schoolType === 1) {
      prevPage.setData({
        postgradSchool: e.currentTarget.dataset.schoolid,
        postgradSchoolName: e.currentTarget.dataset.schoolname
      })
    }
    wx.navigateBack({
      delta: 1
    })
  },
  scrollToBottom(e) {
    console.log(self.data.pageNumber + "/" + self.data.totalPage);
    if (self.data.pageNumber >= self.data.totalPage) {
      return;
    }
    self.setData({
      pageNumber: self.data.pageNumber + 1
    })
    wx.showLoading({
      title: '加载中'
    })
    wx.request({
      url: host + '/lx/mini/user/schools',
      data: {
        kw: self.data.keyword,
        country: self.data.countryArray[self.data.countryIndex],
        page: self.data.pageNumber
      },
      success: function (res) {
        if (res.data.status == 200) {
          var list = self.data.list.concat(res.data.info.list);
          self.setData({
            list: list,
            pageNumber: res.data.info.pageNumber,
            totalPage: res.data.info.totalPage
          });
        } else {
          console.log("error: " + res.data.msg);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  }
})